package edu.hnu.tradeserver.service.impl;

import static edu.hnu.tradeserver.common.util.TimeUtil.calculateStartDate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hnu.commonutils.Result;
import edu.hnu.tradeserver.entity.Transaction;
import edu.hnu.tradeserver.service.TransactionService;
import edu.hnu.tradeserver.mapper.TransactionMapper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Comparator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 64132
* @description 针对表【transaction】的数据库操作Service实现
* @createDate 2023-08-06 17:38:24
*/
@Service
public class TransactionServiceImpl extends ServiceImpl<TransactionMapper, Transaction>
    implements TransactionService{

    @Autowired
    TransactionMapper transactionMapper;

    @Override
    public Result transactionInfo() {
        System.out.println("getStocksInfo");
        QueryWrapper<Transaction> queryWrapper = new QueryWrapper<>();
        List<Transaction> transactionList = transactionMapper.selectList(queryWrapper);
        return Result.ok().data(transactionList);
    }

    private List<Transaction> getTransactionInfoByTimeRange(String timeRange) {
        QueryWrapper<Transaction> queryWrapper = new QueryWrapper<>();

        // 根据时间范围设置查询条件
        LocalDateTime endDate = LocalDateTime.now().with(LocalTime.MAX);;  // 结束日期为当前日期
        LocalDate startDate = calculateStartDate(timeRange, endDate.toLocalDate());  // 计算起始日期


        System.out.println("startDate = " + startDate + "endDate = " + endDate);
        queryWrapper.between("transaction_date", startDate.atStartOfDay(), endDate);

        List<Transaction> transactionList = transactionMapper.selectList(queryWrapper);

        return transactionList;
    }

    @Override
    public List<Transaction> getTransactionInfoByCondition(String timeRange, String sortOrder,
        String sortBy) {
        List<Transaction> transactionList = getTransactionInfoByTimeRange(timeRange);

        // 根据排序字段和排序方式进行排序
        Comparator<Transaction> comparator;
        switch (sortBy) {
            case "Date":
                comparator = Comparator.comparing(Transaction::getTransactionDate);
                break;
            case "ClientName":
                comparator = Comparator.comparing(Transaction::getTransactionClientName);
                break;
            case "ClientSide":
                comparator = Comparator.comparing(Transaction::getTransactionType);
                break;
            case "Ticker":
                comparator = Comparator.comparing(Transaction::getTransactionTicker);
                break;
            case "RIC":
                comparator = Comparator.comparing(Transaction::getTransactionRic);
                break;
            case "Size":
                comparator = Comparator.comparing(Transaction::getTransactionQuantity);
                break;
            case "Price":
                comparator = Comparator.comparing(Transaction::getTransactionPrice);
                break;
            case "NotionalUSD":
                comparator = Comparator.comparing(Transaction::getTransactionCurrency);
                break;
            case "Currency":
                comparator = Comparator.comparing(Transaction::getTransactionCurrency);
                break;
            case "IssuerSector":
                comparator = Comparator.comparing(Transaction::getTransactionIssuerSector);
                break;
            case "SalesPerson":
                comparator = Comparator.comparing(Transaction::getTraderId);
                break;
            case "HT/PT":
                comparator = Comparator.comparing(Transaction::getTransactionHtPt);
                break;
            default:
                throw new IllegalArgumentException("Invalid sortBy field: " + sortBy);
        }


        if ("desc".equals(sortOrder)) {
            comparator = comparator.reversed();
        }

        transactionList.sort(comparator);

        return transactionList;
    }





}












